package com.fenghongzhang.music.fragment;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fenghongzhang.music.R;
import com.fenghongzhang.music.adapter.MusicAdapter;
import com.fenghongzhang.music.entity.Music;
import com.fenghongzhang.music.service.MusicService;
import com.fenghongzhang.music.util.MusicModel;
import com.fenghongzhang.music.util.MusicUtils;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends Fragment {

    private ListView musiclist;

    private static MusicService.MyBinder binder;


    private Button pause;
    private Button pre;
    private Button next;
    private Button restart;

    private Button model;

    private static final String TAG = "MusicFragment";


    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if(msg.what == 101){
                binder.callPause();
            }else if(msg.what == 102){
                binder.callRestart();
            }

        }
    };

    public MusicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_music, container, false);



        model = (Button) inflate.findViewById(R.id.model);


        musiclist = (ListView) inflate.findViewById(R.id.musiclist);
        pause = (Button) inflate.findViewById(R.id.pause);
        pre = (Button) inflate.findViewById(R.id.pre);
        next = (Button) inflate.findViewById(R.id.next);

        restart = (Button) inflate.findViewById(R.id.restart);


        List<Music> musics = MusicUtils.getMusics(getActivity());

        MusicAdapter musicAdapter = new MusicAdapter(musics,getActivity());

        musiclist.setAdapter(musicAdapter);



        //弹出一个popupwindow
        model.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final PopupWindow popupWindow = new PopupWindow(getActivity());


                View inflate1 = LayoutInflater.from(getActivity()).inflate(R.layout.pop_window_ayout, null);

                final TextView random = inflate1.findViewById(R.id.random);
                random.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i(TAG, "onClick: ----");

                        //其实修改播放模式,就是修改播放下一首的逻辑.
                        //下一首的逻辑,就是怎么产生position的问题.
                        MusicService.model = MusicModel.RANDOM;
                        popupWindow.dismiss();
                    }
                });
                

                //三要素
                popupWindow.setContentView(inflate1);
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

                popupWindow.setOutsideTouchable(true);

                popupWindow.showAsDropDown(model,0,-300);


            }
        });

        //启动服务
        Intent intent = new Intent(getActivity(), MusicService.class);
        getActivity().startService(intent);

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MyBinder)service;
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        //绑定
        getActivity().bindService(intent,connection, Context.BIND_AUTO_CREATE);


        musiclist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, "onItemClick: "+position);
                //播放的功能要.要放到了service,不在活动页面
                binder.callPlay(position);
            }
        });


        //暂停音乐播放
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callPause();
            }
        });

        restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callRestart();
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callPlayNext();
            }
        });

        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.callPlayPre();
            }
        });

        return inflate;
    }

}
